﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Com.CMS263.PO;
using Com.CMS263.BLL;
using Com.CMS263.Util;
namespace Com.CMS263.Web.manage.adminUser
{
    public partial class AddUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.bindRole();
                this.bindUserList();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            AdminUserBLL aub = new AdminUserBLL();
            string userName = StringUtil.checkStr(this.txtUserName.Text);
            string password = StringUtil.checkStr( this.txtPassword.Text);
            string userEmail = StringUtil.checkStr(this.txtUserEmail.Text);

            if (userName == null || userName.Length ==0)
            {
                StringUtil.Alert("请填写用户名","-1");
            }
            else if (userName.Length > 50)
            {
                StringUtil.Alert("用户名最多50个字符","-1");
            }else  if (password == null || password.Length == 0)
            {
                StringUtil.Alert("请填写密码", "-1");
            }
            else if (password.Length > 50)
            {
                StringUtil.Alert("密码最多32个字符", "-1");
            }
            else if (aub.isExist(userName))
            {
                StringUtil.Alert("此用户名已经存在", "-1");
            }
            else if(string.IsNullOrEmpty(userEmail))
            {
                StringUtil.Alert("请输入邮箱", "-1");
            }
            int roleID = int.Parse(this.dropRole.SelectedValue);
            AdminUser au = new AdminUser();
            au.userName = userName;
            au.roleID = roleID;
           // au.UserEmail = userEmail;
            au.password = StringUtil.Md5(password);
            
            
            aub.Add(au);

            AdminLoginInfo ali = AdminLoginCookie.GetLoginInfo();
            ArtTypeBLL atbll = new ArtTypeBLL();
           

            bindUserList();
            bindRole();

        }
        void bindUserList()
        { 
            AdminUserBLL aub=new AdminUserBLL();
            IList<UserList> list = aub.GetUserList();
            this.userList.DataSource = list;
            this.userList.DataBind();

        }
        void bindRole()
        {
            AdminRoleBLL urb = new AdminRoleBLL();
            this.dropRole.DataSource = urb.GetAllAdminRole();
            this.dropRole.DataTextField = "roleName";
            this.dropRole.DataValueField = "id";
            this.dropRole.DataBind();
        }
        protected void deleteUser(object sender, EventArgs e)
        {
            int  id = int.Parse(((LinkButton)sender).CommandArgument);
            AdminUserBLL aub = new AdminUserBLL();
            AdminUser ab = aub.GetAdminUserByID(id);
            aub.DeleteUserById(id);
            

            AdminLoginInfo ali = AdminLoginCookie.GetLoginInfo();
            ArtTypeBLL atbll = new ArtTypeBLL();
           

            this.bindRole();
            this.bindUserList();


        }
    }
}
